<?php
use Symfony\Component\Yaml\Yaml;
class TbfI18n {
    /**
     * @param string $key
     * @return string
     */
    public static function tran($key){
        $config = self::getTranslateConfig();
        if (empty($config[$key])){
            return $key;
        }
        return $config[$key];
    }

    private static $translateCache = array();
    public static function tranByLangCode($key,$langCode){
        if (empty(self::$translateCache[$langCode])){
            self::$translateCache[$langCode] = self::loadTranslateByLanguageCode($langCode);
        }
        if (empty(self::$translateCache[$langCode][$key]) ){
            return $key;
        }
        return self::$translateCache[$langCode][$key];
    }

    private static $defaultCountryCode='Other';
    public static function getCurrentCountryCode(){
        //分析ip
        $ip = TbfInput::getIp();
        $isoCode = TbfIp::getCountryIsoCode($ip);
        if (!self::isCountryCodeExist($isoCode)){
            return self::$defaultCountryCode;
        }
        return $isoCode;
    }
    private static $defaultCurrencyCode = 'Other';
    //国家货币
    static function getCurrentCurrencyCode(){
        $code = self::getCurrentCountryCode();
        if ($code===self::$defaultCountryCode){
            return self::$defaultCurrencyCode;
        }
        return self::getCountryCurrencyMap()[$code];
    }

    private static $CountryCurrencyMap;
    static function getCountryCurrencyMap(){
        if (!empty(self::$CountryCurrencyMap)){
            return self::$CountryCurrencyMap;
        }
        self::$CountryCurrencyMap = TbfEncoding::readJsonFile(
            TbfConfig::$TbfResourcePath.'/I18n/CountryCurrencyMap.json');
        return self::$CountryCurrencyMap;
    }
    private static $CountryCodeList = null;
    //返回2个字母的国家列表
    static function getCountryCodeList(){
        if (!empty(self::$CountryCodeList)){
            return self::$CountryCodeList;
        }
        return self::$CountryCodeList = array_keys(self::getCountryCurrencyMap());
    }
    static function isCountryCodeExist($code){
        $map = self::getCountryCurrencyMap();
        return !empty($map[$code]);
    }
    private static $CurrencyCodeList = null;
    //返回3个字母的货币列表
    static function getCurrencyCodeList(){
        if (!empty(self::$CurrencyCodeList)){
            return self::$CurrencyCodeList;
        }
        return self::$CurrencyCodeList = array_unique(array_values(self::getCountryCurrencyMap()));
    }
    static function isCurrencyCodeExist($code){
        return in_array($code,self::getCurrencyCodeList());
    }
    function getCurrencyByCountryCode($countryCode){
        $map = self::getCountryCurrencyMap();
        return $map[$countryCode];
    }

    public static $defaultCode = 'en';
    private static $languageCode = null;
    public static function setLanguageCode($code){
        $config = self::getLanguageConfig();
        if (empty($config[$code])){
            throw new \Exception('OwxLanguage setLanguageCode ['.$code.'] not exist');
        }
        if (self::$languageCode!=$code) {
            self::$currentTranslateCache = null;
        }
        self::$languageCode = $code;
    }

    //设置code,但是不报错,返回是否设置成功
    private static function setLanguageCodeIgnoreNotExist($code){
        $config = self::getLanguageConfig();
        if (empty($config[$code])){
            return false;
        }
        if (self::$languageCode!=$code) {
            self::$currentTranslateCache = null;
        }
        self::$languageCode = $code;
        return true;
    }

    /**
     * 语言代码来源有下列4个地方(按照选择优先级)
     * 1.代码设置语言
     * 2.传入参数设置语言
     * 3.cookie 里面设置语言
     * 4.http头里面的 accept-language 设置语言
     * 如果输入有错误,忽略掉错误,采用默认值.
     * @return null
     */
    public static function getLanguageCode(){
        //代码设置语言(顺便缓存)
        if (self::$languageCode!==null){
            return self::$languageCode;
        }
        //传入参数设置语言
        $code = TbfInput::inStr('language');
        if (!empty($code)){
            if (self::setLanguageCodeIgnoreNotExist($code)){
                return self::$languageCode;
            }
        }
        //cookie 里面设置的语言
        $code = TbfCookie::getStr('language');
        if (!empty($code)){
            if (self::setLanguageCodeIgnoreNotExist($code)){
                return self::$languageCode;
            }
        }
        //accept-language设置语言
        $httpLangMap = array();
        foreach(self::getLanguageConfig() as $code=>$v1){
            foreach($v1['HttpNameList'] as $name){
                $httpLangMap[$name] = $code;
            }
        }
        $name = TbfInput::matchLanguageFromList(array_keys($httpLangMap));
        if ($name===null){
            self::setLanguageCode(self::$defaultCode);
            return self::$languageCode;
        }
        $code = $httpLangMap[$name];
        self::setLanguageCode($code);
        return self::$languageCode;
    }

    public static function getLanguageConfig(){
        $content = TbfConfig::$ConfigPath.'/language.yml';
        return Yaml::parse($content);
    }
    private static $currentTranslateCache = null;
    private static function getTranslateConfig(){
        if (!empty(self::$currentTranslateCache)){
            return self::$currentTranslateCache;
        }
        self::$currentTranslateCache = self::loadTranslateByLanguageCode(self::getLanguageCode());
        return self::$currentTranslateCache;
    }

    private static function loadTranslateByLanguageCode($langCode){
        $tbfTablePath = TbfConfig::$TbfResourcePath.'/I18n/'.$langCode.'.json';
        $tbfTable = TbfEncoding::readJsonFile($tbfTablePath);
        $output = $tbfTable;

        $userTablePath = TbfConfig::$ConfigPath.'/Translate/.'.$langCode.'.yml';
        if (file_exists($userTablePath)){
            $content = file_get_contents($userTablePath);
            $userTable = Yaml::parse($content);
            $output = array_merge($output,$userTable);
        }
        return $output;
    }

    public static function clearCache(){
        self::$languageCode = null;
        self::$currentTranslateCache = null;
        self::$CountryCurrencyMap = null;
    }
}